// generated 2005/9/8 12:03:41 CST by xihels@163.com
//

#if defined __GNUC__ && __GNUC__ < 3
#error This program will crash if compiled with g++ 2.x
// see the dynamic_cast bug in the gtkmm FAQ
#endif //
#include "config.h"
#include <gtkmmconfig.h>
#if GTKMM_MAJOR_VERSION==2 && GTKMM_MINOR_VERSION>2
#define GMM_GTKMM_22_24(a,b) b
#else //gtkmm 2.2
#define GMM_GTKMM_22_24(a,b) a
#endif //
#include "CycloneDialog.h"
#include <gdk/gdkkeysyms.h>
#include <gtkmm/accelgroup.h>
#include <gtkmm/alignment.h>
#include "MainWindow.h"
#include "pixmaps.h"

CycloneDialog::CycloneDialog(
	MainWindow& _parent,
	uint32_t _id,
	const Glib::ustring& title, 
	const Glib::ustring& message,
	bool use_markup,
	bool modal,
	Gtk::MessageType type) :
    parent(_parent),
    id(_id)
{ 
    CycloneDialog* cycloneDialog = this;
    gmm_data = new GlademmData(get_accel_group());
    Gtk::Image* image;
    switch (type) {
	case Gtk::MESSAGE_INFO:
	    image = Gtk::manage(new class Gtk::Image(Gtk::StockID("gtk-dialog-info"),
			Gtk::IconSize(6)));
	    break;
	case Gtk::MESSAGE_ERROR:
	    image = Gtk::manage(new class Gtk::Image(Gtk::StockID("gtk-dialog-error"),
			Gtk::IconSize(6)));
	    break;
	case Gtk::MESSAGE_WARNING:
	    image = Gtk::manage(new class Gtk::Image(Gtk::StockID("gtk-dialog-warning"),
			Gtk::IconSize(6)));
	    break;
	case Gtk::MESSAGE_QUESTION:
	    image = Gtk::manage(new class Gtk::Image(Gtk::StockID("gtk-dialog-question"),
			Gtk::IconSize(6)));
	    break;
    }

    Gtk::Label* messageLabel = Gtk::manage(new class Gtk::Label(message, use_markup));

    Gtk::HBox* hbox = Gtk::manage(new class Gtk::HBox(false, 0));

    hbuttonbox = 
	Gtk::manage(new class Gtk::HButtonBox(Gtk::BUTTONBOX_END, 0));

    Gtk::VBox* vbox = Gtk::manage(new class Gtk::VBox(false, 0));

    image->set_size_request(78,73);
    messageLabel->set_alignment(0.1,0.3);
    hbox->pack_start(*image);
    hbox->pack_start(*messageLabel);
    hbuttonbox->set_border_width(7);
    vbox->pack_start(*hbox);
    vbox->pack_start(*hbuttonbox, Gtk::PACK_SHRINK, 0);
    cycloneDialog->set_title(title);
    cycloneDialog->set_modal(modal);
    cycloneDialog->property_window_position().set_value(Gtk::WIN_POS_CENTER_ALWAYS);
    cycloneDialog->set_type_hint(Gdk::WINDOW_TYPE_HINT_TOOLBAR);
    cycloneDialog->set_resizable(true);
    cycloneDialog->add(*vbox);

}

CycloneDialog::~CycloneDialog()
{
    delete gmm_data;
}

PasswdErrorDialog::PasswdErrorDialog(
	MainWindow& _parent,
	uint32_t _id,
	const Glib::ustring& message,
	bool use_markup ,
	bool modal) :
    CycloneDialog(_parent,
	    _id,
	    "登录出错", 
	    message,
	    use_markup,
	    modal,
	    Gtk::MESSAGE_ERROR)
{
    Gtk::Button* button = Gtk::manage(new class Gtk::Button(Gtk::StockID("gtk-ok")));
    button->set_flags(Gtk::CAN_DEFAULT);

    hbuttonbox->pack_start(*button);
    this->set_default(*button);
    
    button->add_accelerator("clicked",
	   gmm_data->getAccelGroup(), GDK_Escape, Gdk::ModifierType(0), Gtk::ACCEL_VISIBLE);
    this->show_all();

    button->add_accelerator("clicked",
	   gmm_data->getAccelGroup(), GDK_Escape, Gdk::ModifierType(0), Gtk::ACCEL_VISIBLE);
    
    button->signal_clicked().connect(
	    sigc::bind<PasswdErrorDialog*>(
	    sigc::mem_fun(parent, &MainWindow::on_passwdErrorDialog_ok), this),
	    false);
}

bool PasswdErrorDialog::on_delete_event(GdkEventAny *)
{
    parent.on_passwdErrorDialog_ok(this);
    return false;
}

DisconnectDialog::DisconnectDialog(
	MainWindow& _parent,
	uint32_t _id,
	const Glib::ustring& message,
	bool use_markup ,
	bool modal) :
    CycloneDialog(_parent, _id,
	    "连接断开",
	    message, 
	    use_markup,
	    modal, 
	    Gtk::MESSAGE_QUESTION),
    time_out(15)
{
    labelYes = Gtk::manage(new class Gtk::Label("15秒后重新连接(_Y)", true));
    
    Gtk::Image* imageYes = 
	Gtk::manage(new class Gtk::Image(Gtk::StockID("gtk-yes"), Gtk::IconSize(4)));
    Gtk::Alignment* alignmentYes = 
	Gtk::manage(new class Gtk::Alignment(0.5, 0.5, 0, 0));
    Gtk::HBox* hboxYes = 
	Gtk::manage(new class Gtk::HBox(false, 2));
    
   
    Gtk::Button* buttonYes = Gtk::manage(new class Gtk::Button());

    hboxYes->pack_start(*imageYes, Gtk::PACK_SHRINK, 0);
    hboxYes->pack_start(*labelYes, Gtk::PACK_SHRINK, 0);
    alignmentYes->add(*hboxYes);

    buttonYes->add(*alignmentYes);

    Gtk::Button* buttonNo  = Gtk::manage(new class Gtk::Button(Gtk::StockID("gtk-no")));

    buttonNo->add_accelerator("clicked",
	   gmm_data->getAccelGroup(), GDK_Escape, Gdk::ModifierType(0), Gtk::ACCEL_VISIBLE);

    hbuttonbox->pack_start(*buttonYes);
    hbuttonbox->pack_start(*buttonNo);

    this->show_all();

    buttonNo->add_accelerator("clicked",
	   gmm_data->getAccelGroup(), GDK_Escape, Gdk::ModifierType(0), Gtk::ACCEL_VISIBLE);

    buttonYes->signal_clicked().connect(
	    sigc::bind<DisconnectDialog*>(
		sigc::mem_fun(parent, &MainWindow::on_disconnectDialog_yes), this),
	    false);

    buttonNo->signal_clicked().connect(
	    sigc::bind<DisconnectDialog*>(
		sigc::mem_fun(parent, &MainWindow::on_disconnectDialog_no), this),
	    false);

    Glib::signal_timeout().connect(sigc::mem_fun(*this, &DisconnectDialog::on_timeout), 1000);
}


bool DisconnectDialog::on_delete_event(GdkEventAny *)
{
    parent.on_disconnectDialog_no(this);
    return false;
}

bool DisconnectDialog::on_timeout()
{
    time_out--;
    if (0 == time_out) {
	parent.on_disconnectDialog_yes(this);
	return false;
    } else {
	char text[32];
	snprintf(text, 32, "%02d秒后重新连接(_Y)", time_out);
	labelYes->set_label(text);
	return true;
    }
}
